import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UpdateDemo {
    public static void main(String[] args) {
        int rs = UpdateDemo.update();
        if(rs > 0){
            System.out.println("更行成功");
        }else{
            System.out.println("更新失败");
        }
    }

    private static int update() {
        int result = 0;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            // 1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2.获取数据库链接
            String url = "jdbc:mysql://localhost:3306/jdbctest?zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai";
            con = DriverManager.getConnection(url, "root", "root");
            // 3.1sql
            String sql = "update user set username='奥斯卡', password='987',age=23 where username='宁荣荣'";
            // 3.2执行sql语句对象
            pstmt = con.prepareStatement(sql);
            // 3.3执行sql
            result = pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                pstmt = null;
            }
            if(con != null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                con = null;
            }
        }
        

        return result;
    }
}
